<template>
  <div>
    <el-dialog
      title="新增设备检定"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form labelWidth="150px">
        <el-form-item label="设备名称" class="is-required">
          <selecteq :selecteqid.sync="selecteqid" v-if="isselecteq"></selecteq>
          <div class="el-form-item__error" v-if="!$v.selecteqid.id.required">
            请选择设备名称
          </div>
        </el-form-item>
        <el-form-item label="溯源方式" class="is-required">
          <el-input v-model="newscheduleData.traceability"></el-input>
          <!-- <el-select placeholder="筛选溯源方式" style="width:100%" @change="changesyfs">
            <el-option
              v-for="item in syfsArr"
              :key="item.value"
              :label="item.hash.code_name"
              :value="item.hash.code_name"
            ></el-option>
          </el-select> -->
          <div class="el-form-item__error" v-if="!$v.newscheduleData.traceability.required">
            请输入溯源方式
          </div>
        </el-form-item>
        <el-form-item label="检定/校准单位" class="is-required">
          <el-input v-model="newscheduleData.verifiy_company"></el-input>
          <el-select placeholder="筛选检定/校准单位" style="width:100%" @change="changejddw">
            <el-option
              v-for="item in jddwArr"
              :key="item.value"
              :label="item.hash.code_name"
              :value="item.hash.code_name"
            ></el-option>
          </el-select>


          <div class="el-form-item__error" v-if="!$v.newscheduleData.verifiy_company.required">
            请输入检定/校准单位
          </div>
        </el-form-item>

        <el-form-item label="计划检定/校准日期" class="is-required">
          <el-date-picker
            valueFormat="yyyy-MM-dd"
            v-model="newscheduleData.verifiytime"
            type="date"
            style="width:100%"
            placeholder="选择日期"
          >
          </el-date-picker>
          <div class="el-form-item__error" v-if="!$v.newscheduleData.verifiytime.required">
            请选择计划检定/校准日期
          </div>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button size="mini" @click="dialogVisible = false">取 消</el-button>
        <el-button size="mini" type="primary" @click="sumitedit">新 增</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import selecteq from '~/components/common/selecteq.vue';
import { required } from 'vuelidate/lib/validators';

export default {
  validations: {
    selecteqid: {
      id: {
        required,
      },
    },
    newscheduleData: {
      traceability: {
        required,
      },
      verifiy_company: {
        required,
      },
      verifiytime: {
        required,
      },
    },
  },
  data() {
    return {
      ts: this.newStore(),
      dialogVisible: true,
      newscheduleData: {
        traceability: '',
        verifiy_company: '',
      },
      jddwstr: '',
      isselecteq: true,
      selecteqid: {},
      jddwArr: [],
      // syfsArr: [],
    };
  },
  watch: {
    selecteqid(newval) {
      this.newscheduleData.traceability = '';
      this.newscheduleData.verifiy_company = '';
      const req = {
        handle: 'DD5D108B99FC12B72DC0B97F927BBE3DD7F2412F8A4ED57CBFF0B9210751F692',
        id: newval.id,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this.newscheduleData.traceability = ts.d.data.operation1[0].hash.traceability;
        this.newscheduleData.verifiy_company = ts.d.data.operation1[0].hash.verifiy_company;
      });
    },
  },
  components: {
    selecteq,
  },
  created() {
    this.getcodeData('0405', '04', 'jddwArr');
    // this.getcodeData('0406', '04', 'syfsArr');
  },
  methods: {
    changejddw(val) {
      this.newscheduleData.verifiy_company = val;
    },
    // changesyfs(val) {
    //   this.newscheduleData.traceability = val;
    // },
    // 公共方法获取代码项
    getcodeData(supid, codetype, arrname) {
      const req = {
        handle: '67A5B6FEF17D97DECF116BDF95B95C0B167942DD3F1604F9',
        supid,
        codetype,
      };
      this.spost(this.ts, '/mainservlet', req).then((ts) => {
        this[arrname] = ts.d.data.operation1;
      });
    },
    reload() {
      this.isselecteq = false;
      this.$nextTick(() => {
        this.isselecteq = true;
      });
    },
    // 提交编辑设备
    sumitedit() {
      if (this.$v.$invalid) {
        this.$message({
          type: 'error',
          message: ' 请仔细核对填写信息',
        });
      } else {
        this.$confirm('确认保存?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        })
          .then(() => {
            const req = {
              handle: 'DD5D108B99FC12B782127AB5198E65DA50B1E3C352635A7A21E0422975959501A52885BC247C5A4B',
              eid: this.selecteqid.id,
              traceability: this.newscheduleData.traceability,
              verifiy_company: this.newscheduleData.verifiy_company,
              verifiytime: this.newscheduleData.verifiytime,
            };

            this.spost(this.ts, '/mainservlet', req).then(() => {
              this.$message({
                type: 'success',
                message: '保存成功',
              });
              this.$parent.getschedulelist();
              this.dialogVisible = false;
            });
          })
          .catch(() => {
            this.$message({
              type: 'info',
              message: '已取消',
            });
          });
      }
    },
  },
};
</script>
